<?php
/**
 * Model_Members
 *  
 * @author Administrator
 * @version 
 */
class Model_CommonMembers extends Zend_Db_Table_Abstract
{
    /**
     * The default table name
     */
    protected $_name = 'tcl_common_members';
    
    //通过用户名取 数组 
    public function getUserByAccount($account){
        
        $db = $this->getAdapter();
        $select = $db->select();
        
        $select->from($this->_name, array('uid', 'username', 'email', 'password', 'phone', 'company_id', 'question', 'answer'))
               	->where('username = ? OR phone = ? OR email = ?', $account);
        
        try {
            $row = $db->fetchRow($select);
        } catch (Exception $e) {
            echo $e;
            exit();
        }
        /*
        print_r($row);
        exit();
        */
        return $row;
    }
    
    //注册
    public function setRegisterPost($data){
    
        //$data = array('title'=>'title');
        //$id = $this->insert($data);
        try {
            $id = $this->insert($data);
        } catch(Exception $e) {
            // when it fails to insert
            // how can i run this echo
            echo $e; //??
            exit();
        }

        return $id;
    }
   
    public function getProfile($uid) {
        $db = $this->getAdapter();
        $select = $db->select();
        
        $select->from(array('t1'=>$this->_name), array('uid', 'username', 'email', 'phone', 'company_id', 'groupid', 'lastloginip', 'lastlogindate','ban'))
        		->from(array('t2'=>'tcl_common_group'), array('id','value','level'))
        		->where('t1.groupid = t2.id')
                ->where('t1.uid = ?', $uid);
        
        
        try {
            $row = $db->fetchRow($select);
        } catch (Exception $e) {
            echo $e;
            exit();
        }
        /*
         print_r($row);
        exit();
        */
        $core = new Model_Core();
        $row[lastlogin] = str_replace('-', ' ', $core->convertip($row[lastloginip]));
        
        $msg = new Model_CommonMsg();
        $row[is_msg] = $msg->getIsMsg($uid);

        return $row;
    }
    
    
    public function getByUid($uid) {
                
        $row = $this->find($uid)->toArray();
        
        return $row[0];
    }
    
    
    //找回密码  写入SALT
    public function setForgotSalt($uid, $salt) {
        
        $forgot_date = date("YmdH", time());
        $db = $this->getAdapter();
        
        $set = array(
            'forgot_salt'     => $salt,
            'forgot_date'     => $forgot_date,
            'fotgot_status'   => 1,
        );
        
        $where = $db->quoteInto('uid = ?', $uid);
        
        $row = $this->update($set, $where);
        
        return $row;
    }
    
    //邮件找回 状态
    public function setForgotUsed($uid) {
        $db = $this->getAdapter();
        
        $set = array(
            'fotgot_status'   => 0,
        );
        
        $where = $db->quoteInto('uid = ?', $uid);
        
        $row = $this->update($set, $where);
        
        return $row;
    }
    
    
    //设置新密码
    public function setNewPassword($uid, $password) {
        $db = $this->getAdapter();
        
        $set = array(
            'password'   => $password,
        );
        
        $where = $db->quoteInto('uid = ?', $uid);
        
        $row = $this->update($set, $where);
        
        return $row;
    }
    
    
    //更新密码安全
    public function setMemberProfile($data) {
    	
        $db = $this->getAdapter();
        $core = new Model_Core();
        $member = $core->isLogin();
        
        $where = $db->quoteInto('uid = ?', $member[uid]);
        try {
        	$id = $this->update($data, $where);
        } catch(Exception $e) {
        	// when it fails to insert
        	// how can i run this echo
        	echo $e; //??
        	exit();
        }
        return $id;
    }
    
    //上次登陆IP
    public function setLastLogin($uid, $ip) {
        $db = $this->getAdapter();
        
        $data = array(
        	'lastloginip' 	=> $ip,
        	'lastlogindate' => time(),
        );
        
        $where = $db->quoteInto('uid = ?', $uid);
        try {
        	$id = $this->update($data, $where);
        } catch(Exception $e) {
        	// when it fails to insert
        	// how can i run this echo
        	echo $e; //??
        	exit();
        }
        return $id;
    }
    
	//会员列表
    public function getMemberList($page, $url, $app, $query, $count=30) {
    
    	$core = new Model_Core();
    	$db = $this->getAdapter();
    	$select = $db->select();
    
    	$select->from($this->_name, array('uid','username','regip', 'regdate', 'lastlogindate', 'ban'));
    	if ($app != -1){
    	    $select->where('ban = ?', $app);
    	}
    	if ($query){
            $select->where("uid like ? OR username like ? OR email like ? OR phone like ?", "%$query%");
    	}
    	
    	$select->order(array('regdate DESC'));
    
    	$how = count($db->fetchAll($select));
    	//echo $how;
    	//print_r($db->fetchAll($select));
    
    	$howpage = ceil($how / $count);
    	$offset	 = $page * $count - $count;
    
    	$multipage = $core->html_multi($page, $howpage, $url);
    
    	$select->limit($count, $offset);
    	
    	
    	try {
    		$row = $db->fetchAll($select);
    	} catch (Exception $e) {
    		echo $e;
    		exit();
    	}
    
    	$results = array(
    			'row' => $row,
    			'mulitpage' => $multipage,
    	);
    	return $results;
    }
    
    
    //BANED USER
    public function setBan($uid) {

        $db = $this->getAdapter();
        $core = new Model_Core();
        $member = $core->isLogin();
        
        if ($member[groupid] == 1){
            $data = array(
            		'ban' => 2,
            );
            
            $where = $db->quoteInto('uid = ?', $uid);
            try {
            	$id = $this->update($data, $where);
            } catch(Exception $e) {
            	// when it fails to insert
            	// how can i run this echo
            	echo $e; //??
            	exit();
            }
            
            return $id;
        }
        
    }
    

    public function setReBan($uid) {
    
    	$db = $this->getAdapter();
    	$core = new Model_Core();
    	$member = $core->isLogin();
    
    	if ($member[groupid] == 1){
    		$data = array(
    			'ban' => 1,
    		);
    
    		$where = $db->quoteInto('uid = ?', $uid);
    		try {
    			$id = $this->update($data, $where);
    		} catch(Exception $e) {
    			// when it fails to insert
    			// how can i run this echo
    			echo $e; //??
    			exit();
    		}
    
    		return $id;
    	}
    
    }
    
}
